package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
//import com.itheima.constant.RedisConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import redis.clients.jedis.JedisPool;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 体检套餐服务
 */
@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private JedisPool jedisPool;

    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @Value("${out_put_path}")//从属性文件读取输出目录的路径
    private String outputpath;

    //新增套餐信息，同时需要关联检查组
    public void add (Setmeal setmeal, Integer[]checkgroupIds){
        setmealDao.add(setmeal);
        Integer setmealId = setmeal.getId();
        this.setSetmealAndCheckgroup(setmealId, checkgroupIds);
        //将图片名称保存到Redis集合中
        String fileName = setmeal.getImg();
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES, fileName);
        //新增套餐后需要重新生成静态页面
        generateMobileStaticHtml();
    }

    //生成静态页面
    public void generateMobileStaticHtml() {
        //准备模板文件中所需的数据
        List <Setmeal> setmealList = this.findAll();
        //生成套餐列表静态页面
        generateMobileSetmealListHtml(setmealList);
        //生成套餐详情静态页面（多个）
        generateMobileSetmealDetailHtml(setmealList);
    }

            //生成套餐列表静态页面
            public void generateMobileSetmealListHtml(List <Setmeal> setmealList) {
                Map <String, Object> dataMap = new HashMap <String, Object>();
                dataMap.put("setmealList", setmealList);
                this.generateHtml("mobile_setmeal.ftl", "m_setmeal.html", dataMap);
            }

            //生成套餐详情静态页面（多个）
            public void generateMobileSetmealDetailHtml(List <Setmeal> setmealList) {
                for (Setmeal setmeal : setmealList) {
                    Map <String, Object> dataMap = new HashMap <String, Object>();
                    dataMap.put("setmeal", this.findById(setmeal.getId()));
                    this.generateHtml("mobile_setmeal_detail.ftl","setmeal_detail_" + setmeal.getId() + ".html",dataMap);
                }
            }
            //通用的方法，用于生成静态页面
            public void generateHtml(String templateName, String htmlPageName, Map <String, Object> dataMap) {
                Configuration configuration = freeMarkerConfigurer.getConfiguration();
                Writer out = null;
                try {
                    // 加载模版文件
                    Template template = configuration.getTemplate(templateName);
                    // 生成数据
                    File docFile = new File(outputpath + "/" + htmlPageName);
                    out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docFile)));
                    // 输出文件
                    template.process(dataMap, out);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (null != out) {
                            out.flush();
                        }
                    } catch (Exception e2) {
                        e2.printStackTrace();
                    }
                }
            }


    @Override
    public PageResult pageQuery (QueryPageBean queryPageBean){
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page <Setmeal> page = setmealDao.findByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }


    //设置套餐和检查组多对多关系，操作t_setmeal_checkgroup
    public void setSetmealAndCheckgroup (Integer setmealId, Integer[]checkgroupIds){
        if (checkgroupIds != null && checkgroupIds.length > 0) {
            for (Integer checkgroupId : checkgroupIds) {
                Map <String, Integer> map = new HashMap <>();
                map.put("setmealId", setmealId);
                map.put("checkgroupId", checkgroupId);
                setmealDao.setSetmealAndCheckGroup(map);
            }
        }
    }


    @Override
    public List <Setmeal> findAll () {
        return setmealDao.findAll();
    }

    @Override
    public Setmeal findById ( int id){
        return setmealDao.findById(id);
    }

    @Override
    public List <Map <String, Object>> findSetmealCount() {
        return setmealDao.findSetmealCount();
    }

}
